package com.corpgovernment.organization.dataloader.db;

import com.corpgovernment.organization.entity.db.MbOrgInfo;
import com.corpgovernment.organization.mapper.MbOrgInfoMapper;
import com.corpgovernment.organization.model.MbOrgInfoModel;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jhxue
 * @create 2022-03-01-14:16
 */
@Component
public class MbOrgInfoLoader {

	@Autowired
	private MbOrgInfoMapper mbOrgInfoMapper;

	public List<MbOrgInfo> selectByOrgIds(List<String> orgIds) {
		if (CollectionUtils.isEmpty(orgIds)) {
			return new ArrayList<>();
		}
		Example example = new Example(MbOrgInfo.class);
		example.createCriteria().andIn("orgId", orgIds).andEqualTo("deleteTime", new Date(0));
		return mbOrgInfoMapper.selectByExample(example);
	}

	public List<MbOrgInfo> selectByBusinessCode(String businessCode) {
		if (StringUtils.isEmpty(businessCode)) {
			return new ArrayList<>();
		}
		Example example = new Example(MbOrgInfo.class);
		example.createCriteria().andEqualTo("businessCode", businessCode).andEqualTo("deleteTime", new Date(0));
		return mbOrgInfoMapper.selectByExample(example);
	}

	public List<MbOrgInfoModel> selectOrgByParentIdList(List<String> parentOrgIdList) {
		if (CollectionUtils.isEmpty(parentOrgIdList)) {
			return Collections.emptyList();
		}
		List<MbOrgInfo> mbOrgInfoList = mbOrgInfoMapper.selectOrgByParentIdList(parentOrgIdList);
		if (CollectionUtils.isEmpty(mbOrgInfoList)){
			return Collections.emptyList();
		}
		return mbOrgInfoList.stream()
				.map(this::convert)
				.collect(Collectors.toList());
	}

	private MbOrgInfoModel convert(MbOrgInfo mbOrgInfo){
		MbOrgInfoModel model = new MbOrgInfoModel();
		model.setAdminUid(mbOrgInfo.getAdminUid());
		model.setBindName(mbOrgInfo.getBindName());
		model.setName(mbOrgInfo.getName());
		model.setBindId(mbOrgInfo.getBindId());
		model.setLevel(mbOrgInfo.getLevel());
		model.setOrgId(mbOrgInfo.getOrgId());
		model.setOrgType(mbOrgInfo.getOrgType());
		model.setContactName(mbOrgInfo.getContactName());
		model.setContactEmail(mbOrgInfo.getContactEmail());
		model.setContactPhone(mbOrgInfo.getContactPhone());
		model.setValid(mbOrgInfo.getValid());
		model.setIsBackground(mbOrgInfo.getIsBackground());
		model.setOrderNum(mbOrgInfo.getOrderNum());
		model.setParentId(mbOrgInfo.getParentId());
		model.setHotelAccnt(mbOrgInfo.getHotelAccnt());
		model.setBusinessCode(mbOrgInfo.getBusinessCode());
		model.setParentBusinessCode(mbOrgInfo.getParentBusinessCode());
		model.setContactAreaCode(mbOrgInfo.getContactAreaCode());
		model.setHotelPpay(mbOrgInfo.getHotelPpay());
		return model;
	}

}

